package com.app.control.user;

import com.alibaba.fastjson.JSON;
import com.app.pojo.*;
import com.app.service.*;
import com.app.tools.Contes;
import com.app.tools.Pages;
import com.mysql.cj.util.StringUtils;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.Appinfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: App_SSM
 * @description: 开发者登录
 * @author: Mr.peng
 * @create: 2020-06-10 17:20
 **/
@Controller
public class Login{

    @Resource
    DevUserService devUserService;

    @Resource
    BackednUserService backednUserService;

    @Resource
    DataDictionaryService dictionaryService;
    @Resource
    AppCategoryService categoryService;
    @Resource
    AppInfoService infoService;
    /**
     * jsp跳转
     * @param name
     * @return
     */
    @RequestMapping("login")
    public String jsps(@RequestParam("name") String name){
        String url;
        if (name.equals("manager")){
            url="backendlogin";
        }else{
            url="devlogin";
        }
        return url;
    }

    @RequestMapping("/dologin")
    public String devlogin(HttpServletRequest request, Model model){
        String mes=request.getParameter("name");
        if (mes.equals("dev")) {
            String code=request.getParameter("devCode");
            String password=request.getParameter("devPassword");
            DevUser user = devUserService.selectByPrimaryKey(code, password);
            if (user != null) {
                request.getSession().setAttribute(Contes.USER_SESSION,user);
                model.addAttribute("devUserSession",user);
                return "developer/main";
            } else {
                model.addAttribute("error", "账号或密码不正确！");
                return "devlogin";
            }
        }else{
            String code=request.getParameter("userCode");
            String password=request.getParameter("userPassword");
            BackendUser backendUser=backednUserService.selectByPrimaryKey(code,password);
            if (backendUser!=null){
                request.getSession().setAttribute(Contes.BACK_SESSION,backendUser);
                model.addAttribute("userSession",backendUser);
                return "backend/main";
            }else{
                model.addAttribute("error", "账号或密码不正确");
                return "backendlogin";
            }
        }
    }

    @RequestMapping("/{name}/logout")
    public String logout(HttpServletRequest request,@PathVariable String name){
        //注销session
        if (name.equals("dev")){
            request.getSession().removeAttribute(Contes.USER_SESSION);
        }else{
            request.getSession().removeAttribute(Contes.BACK_SESSION);
        }
        return "redirect:/index.jsp";
    }


    @RequestMapping("/dev/flatform/app/list")
    public String list(HttpServletRequest request,Model model,appinfo_two two) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("quernames");
        String pageIndex=request.getParameter("pageIndex");
        //回显参数
        String appstatus=request.getParameter("queryStatus");
        String querySoftwareName=request.getParameter("querySoftwareName");
        String queryFlatformId=request.getParameter("queryFlatformId");
        String queryCategoryLevel1=request.getParameter("queryCategoryLevel1");
        String queryCategoryLevel2=request.getParameter("queryCategoryLevel2");
        String queryCategoryLevel3=request.getParameter("queryCategoryLevel3");

        System.out.println("name-------------"+name);

        AppInfo appInfo=new AppInfo();
        appInfo.setSoftwareName(two.getQuerySoftwareName());
        appInfo.setValueid(two.getQueryStatus());
        appInfo.setFlatformId(two.getQueryFlatformId());
        appInfo.setCategoryLevel1(two.getQueryCategoryLevel1());
        appInfo.setCategoryLevel2(two.getQueryCategoryLevel2());
        appInfo.setCategoryLevel3(two.getQueryCategoryLevel3());

        List<DataDictionary> dictionary=new ArrayList<DataDictionary>();
        dictionary=dictionaryService.listdata();
        List<DataDictionary> dictionary1=new ArrayList<DataDictionary>();
        dictionary1=dictionaryService.applist();
        List<AppCategory> one=new ArrayList<AppCategory>();
        one=categoryService.onelist();
        List<AppCategory> twos=new ArrayList<AppCategory>();
        if (!StringUtils.isNullOrEmpty(queryCategoryLevel1)) {
            twos = categoryService.twolist(Integer.valueOf(queryCategoryLevel1));
        }
        List<AppCategory> three=new ArrayList<AppCategory>();
        if (!StringUtils.isNullOrEmpty(queryCategoryLevel2)) {
            three = categoryService.twolist(Integer.valueOf(queryCategoryLevel2));
        }
        List<AppInfo> list1= new ArrayList<AppInfo>();
        //账户编码
        if (StringUtils.isNullOrEmpty(name)) {
            name = ((DevUser) request.getSession().getAttribute(Contes.USER_SESSION)).getDevCode();
        }
        Pages pages=new Pages();
        //当前页面
        int curr=pages.getCurrentPageNo();
        //页面大小
        int pagesize=pages.getPageSize();
        //总数
        int count=infoService.getcount(name,appInfo);
        //总页数
        pages.setTotalCount(count);
        int sum=pages.getTotalPageCount();

        if (pageIndex!=null){
               curr=Integer.valueOf(pageIndex);
        }
        pages.setTotalPageCount(sum);
        pages.setCurrentPageNo(curr);
        pages.setPageSize(pagesize);
        //控制首页和尾页
        if(curr < 1){
            curr = 1;
        }else if(curr > count){
            curr = count;
        }
        list1=infoService.getlist(curr,pagesize,appInfo);
        model.addAttribute("pages",pages);
        model.addAttribute("appInfoList",list1);
        model.addAttribute("categoryLevel1List",one);
        model.addAttribute("categoryLevel2List",twos);
        model.addAttribute("categoryLevel3List",three);
        model.addAttribute("statusList",dictionary);
        model.addAttribute("flatFormList",dictionary1);

        //回显
        model.addAttribute("querySoftwareName",querySoftwareName);
        model.addAttribute("queryStatus",appstatus);
        model.addAttribute("queryFlatformId",queryFlatformId);
        model.addAttribute("queryCategoryLevel1",queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2",queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3",queryCategoryLevel3);
        System.out.println("终极-------------");
        return "developer/appinfolist";
    }

    @RequestMapping("/manager/backend/app/list")
    public String backlist(){
        return "backend/applist";
    }

    //二级分类
    @RequestMapping("/categorylevellist")
    @ResponseBody
    public String twos(@RequestParam("pid") String id){
        List<AppCategory> lists=new ArrayList<AppCategory>();
        lists = categoryService.twolist(Integer.valueOf(id));
        return JSON.toJSONString(lists);
    }









}
